**************************************************************************************
** title:		dataset coding for 'Is there a `Youthquake'? 						**
** authors:		ruth dassonneville & ian mcallister 								**
** date:		February 2025 														**
** machine:		MacOS Sonoma 14.4													**
** software:	Stata 18 															**
**************************************************************************************

* --------------
* preamble
* --------------

* set working directory and load data 
cd "~/Dropbox/youthquake-replication/data"
use "MPDataset_MPDS2024a_stata14.dta" , clear

* ---------------------
* variable coding
* ---------------------

gen countrywest=1 if countryname=="Australia"
replace countrywest=1 if countryname=="Austria"
replace countrywest=1 if countryname=="Belgium"
replace countrywest=1 if countryname=="Canada"
replace countrywest=1 if countryname=="Denmark"
replace countrywest=1 if countryname=="Finland"
replace countrywest=1 if countryname=="France"
replace countrywest=1 if countryname=="Germany"
replace countrywest=1 if countryname=="Greece"
replace countrywest=1 if countryname=="Iceland"
replace countrywest=1 if countryname=="Ireland"
replace countrywest=1 if countryname=="Italy"
replace countrywest=1 if countryname=="Luxembourg"
replace countrywest=1 if countryname=="Netherlands"
replace countrywest=1 if countryname=="New Zealand"
replace countrywest=1 if countryname=="Norway"
replace countrywest=1 if countryname=="Portugal"
replace countrywest=1 if countryname=="Spain"
replace countrywest=1 if countryname=="Sweden"
replace countrywest=1 if countryname=="Switzerland"
replace countrywest=1 if countryname=="United Kingdom"
replace countrywest=1 if countryname=="United States"

keep if countrywest==1		// only keep western democracies
keep if pervote>=1			// drop minor parties (<1% of the vote)

gen year=date/100
replace year=round(year,1)

gen decade=1940 if year>=1940 & year<=1949
replace decade=1950 if year>=1950 & year<=1959
replace decade=1960 if year>=1960 & year<=1969
replace decade=1970 if year>=1970 & year<=1979
replace decade=1980 if year>=1980 & year<=1989
replace decade=1990 if year>=1990 & year<=1999
replace decade=2000 if year>=2000 & year<=2010
replace decade=2010 if year>=2010 & year<=2020

* code positions on two dimensions, using MARPOR coding as documented here https://manifesto-project.wzb.eu/information/documents/visualizations
gen EconomyLR=(per401 + per402 + per407 + per410 + per414 + per505 + per507 + per702) - (per403 + per404 + per405 + per406 + per409 + per412 + per413 + per415 + per504 + per506 + per701)
gen SocietyLR= (per601 + per603 + per605 + per608 + per704) - (per201 + per202 + per503 + per602 + per604 + per607)
gen immigrationLR=(per601+per608)-(per602+per607)

* ----------------------------------------------------------
* generate position measures on main dimensions
* ----------------------------------------------------------

** recode ideological variables to run from 0 to 10

gen rile_rs = ( rile - (-100) ) / ( (100)-(-100) ) * 10
gen EconomyLR_rs = ( EconomyLR - (-100) ) / ( (100)-(-100) ) * 10
gen SocietyLR_rs = ( SocietyLR - (-100) ) / ( (100)-(-100) ) * 10
gen immigrationLR_rs = ( immigrationLR - (-100) ) / ( (100)-(-100) ) * 10

drop rile EconomyLR SocietyLR immigrationLR
rename rile_rs rile
rename EconomyLR_rs EconomyLR
rename SocietyLR_rs SocietyLR
rename immigrationLR_rs immigrationLR

** gal-tan positions of left and non-left parties

gen left=0 if parfam!=.
replace left=1 if parfam==10		// ecologist
replace left=1 if parfam==20		// left socialist or other left
replace left=1 if parfam==30		// social democratic
replace left=. if parfam==999		// missing

gen leftSocietyLR=left*SocietyLR	// GAL-TAN position for left parties (0 for non left)
gen leftpervote=left*pervote		// vote share for left parties (0 for non left)

bysort country edate: gen leftSocietyLR_w=leftpervote*leftSocietyLR
bysort country edate: egen totalleftSocietyLR_w=sum(leftSocietyLR_w)
bysort country edate: egen leftvotestotal = sum(leftpervote)
bysort country edate: gen leftSocietyLR_w2=totalleftSocietyLR_w/leftvotestotal

drop leftSocietyLR_w
rename leftSocietyLR_w2 leftSocietyLR_w

gen nonleft=0 if parfam!=.
replace nonleft=1 if parfam>=40		// all that are not eco, left, social democratic
replace nonleft=. if parfam==999	// missing

gen nonleftSocietyLR=nonleft*SocietyLR	// GAL-TAN position for non-left parties (0 for left)
gen nonleftpervote=nonleft*pervote		// vote share for non-left parties (0 for  left)

bysort country edate: gen nonleftSocietyLR_w=nonleftpervote*nonleftSocietyLR
bysort country edate: egen totalnonleftSocietyLR_w = sum(nonleftSocietyLR_w)
bysort country edate: egen nonleftvotestotal = sum(nonleftpervote)
bysort country edate: gen nonleftSocietyLR_w2=totalnonleftSocietyLR_w/nonleftvotestotal

drop nonleftSocietyLR_w
rename nonleftSocietyLR_w2 nonleftSocietyLR_w

gen tradleft=0 if parfam!=.
replace tradleft=1 if parfam==30
replace tradleft=1 if parfam==20
replace tradleft=. if parfam==999

gen tradleftSocietyLR=tradleft*SocietyLR	// GAL-TAN position for trad left parties (0 for others)
gen tradleftpervote=tradleft*pervote		// vote share for trade left parties (0 for others)

bysort country edate: gen tradleftSocietyLR_w=tradleftpervote*tradleftSocietyLR
bysort country edate: egen totaltradleftSocietyLR_w=sum(tradleftSocietyLR_w)
bysort country edate: egen tradleftvotestotal = sum(tradleftpervote)
bysort country edate: gen tradleftSocietyLR_w2=totaltradleftSocietyLR_w/tradleftvotestotal

drop tradleftSocietyLR_w
rename tradleftSocietyLR_w2 tradleftSocietyLR_w

gen tradnonleft=0 if parfam!=.
replace tradnonleft=1 if parfam==40		// liberal
replace tradnonleft=1 if parfam==60		// conservative
replace tradnonleft=. if parfam==999	// missing

gen tradnonleftSocietyLR=tradnonleft*SocietyLR	// GAL-TAN position for non-left parties (0 for left)
gen tradnonleftpervote=tradnonleft*pervote		// vote share for non-left parties (0 for  left)

bysort country edate: gen tradnonleftSocietyLR_w=tradnonleftpervote*tradnonleftSocietyLR
bysort country edate: egen totaltradnonleftSocietyLR_w = sum(tradnonleftSocietyLR_w)
bysort country edate: egen tradnonleftvotestotal = sum(tradnonleftpervote)
bysort country edate: gen tradnonleftSocietyLR_w2=totaltradnonleftSocietyLR_w/tradnonleftvotestotal

drop tradnonleftSocietyLR_w
rename tradnonleftSocietyLR_w2 tradnonleftSocietyLR_w


** most progressive non left party (>=5%vote)

gen nonleft5=nonleft
replace nonleft5=0 if nonleft==1 & pervote<5 

gen nonleft5SocietyLR=nonleft5*SocietyLR
replace nonleft5SocietyLR=. if nonleft5SocietyLR==0
bysort country edate: egen minnonleft = min(nonleft5SocietyLR)

** most conservative left party (>=5% vote)

gen left5=left
replace left5=0 if left==1 & pervote<5 

gen left5SocietyLR=left5*SocietyLR
replace left5SocietyLR=. if left5SocietyLR==0
bysort country edate: egen maxleft = max(left5SocietyLR)


* --------------------------------------------
* keep relevant variables and save dataset
* --------------------------------------------

** create country-election level dataset
keep country countryname edate date party partyname partyabbrev parfam EconomyLR SocietyLR rile leftSocietyLR_w nonleftSocietyLR_w tradleftSocietyLR_w tradnonleftSocietyLR_w maxleft minnonleft countrywest year decade 

collapse (mean) EconomyLR SocietyLR rile  leftSocietyLR_w nonleftSocietyLR_w tradleftSocietyLR_w tradnonleftSocietyLR_w maxleft minnonleft  (first) country countryname edate countrywest year decade  , by(date)

** add key variable for merging with WPID data

gen iso="AU" if countryname=="Australia"
replace iso="AT" if countryname=="Austria"
replace iso="BE" if countryname=="Belgium"
replace iso="CA" if countryname=="Canada"
replace iso="DK" if countryname=="Denmark"
replace iso="FI" if countryname=="Finland"
replace iso="FR" if countryname=="France"
replace iso="DE" if countryname=="Germany"
replace iso="GR" if countryname=="Greece"
replace iso="IS" if countryname=="Iceland"
replace iso="IE" if countryname=="Ireland"
replace iso="IT" if countryname=="Italy"
replace iso="LU" if countryname=="Luxembourg"
replace iso="NL" if countryname=="Netherlands"
replace iso="NZ" if countryname=="New Zealand"
replace iso="NO" if countryname=="Norway"
replace iso="PT" if countryname=="Portugal"
replace iso="ES" if countryname=="Spain"
replace iso="SE" if countryname=="Sweden"
replace iso="CH" if countryname=="Switzerland"
replace iso="GB" if countryname=="United Kingdom"
replace iso="US" if countryname=="United States"

gen isoyear=iso+string(year)

collapse (mean) EconomyLR SocietyLR rile  leftSocietyLR_w nonleftSocietyLR_w tradleftSocietyLR_w tradnonleftSocietyLR_w maxleft minnonleft  (first) country countryname countrywest year decade  , by(isoyear)


save "cmp_positions.dta", replace


